/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.views;

import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import pe.edu.pucp.Servicio.Servicio_Cliente;
import pe.edu.pucp.bean.Cliente;
import pe.edu.pucp.bean.Internacional;
import pe.edu.pucp.bean.Nacional;

/**
 *
 * @author LiziTa
 */
public class PanelClienteEliminar extends javax.swing.JPanel {

   public int choice;
    Servicio_Cliente service = Servicio_Cliente.get_instance();
	
	
	PanelClienteEliminar.ClienteTableModel miclienteTM = new PanelClienteEliminar.ClienteTableModel();

	public void Actualizar(){
            miclienteTM.clientes=service.queryAllClientes();
            miclienteTM.fireTableChanged(null);
        }
       
   	
	class ClienteTableModel extends AbstractTableModel{
        
               
        String Titulos[]={"ID","RUC","R.SOCIAL","DIRECCION","REPRESENTANTE","L.CREDITO","DEPARTAMENTO","CIUDAD","DIVISA","PAIS"};
        ArrayList<Cliente>clientes =Servicio_Cliente.get_instance().queryAllClientes();
               
        @Override
        public int getRowCount() {
            return clientes.size();
        }

        @Override
        public int getColumnCount() {
            return 10;
        }

        @Override
        public Object getValueAt(int fil, int col) {
            String res=null;
            Cliente c = clientes.get(fil);
            String tipo=c.getTipo();
            
            switch (col){
                case 0 : res=""+c.getId();break;
                case 1 : res= c.getRUC();break;
                case 2 : res= c.getRazonSocial();break;
                case 3 : res= c.getDireccion();break;
                case 4 : res= c.getRepresentante();break;
                case 5 : res=""+c.getLineaCred(); break;
                case 6 : if (c instanceof Nacional) {
                         res=((Nacional)c).getDepartamento();
                         break;
                        }
                else {
                    res="       -";
                    break;
                }
                case 7 : if (c instanceof Nacional) {
                         res=((Nacional)c).getCiudad();
                         break;
                        }
                else {
                    res="    -";
                    break;
                }
                case 8 : if (c instanceof Internacional) {
                         res=""+((Internacional)c).getDivisa();
                         break;
                        }
                else {
                    res="     -";
                    break;
                }
                case 9 : if (c instanceof Internacional) {
                         res=((Internacional)c).getPais();
                         break;
                        }
                else {
                    res="     -";
                    break;
                }
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return Titulos[col];
        }
    }
        
    public PanelClienteEliminar() {
        initComponents();
        TablaCliente.setModel(miclienteTM);
     
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaCliente = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Cliente"));

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/pucp/views/logo.gif"))); // NOI18N

        TablaCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaClienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaCliente);

        jLabel1.setText("Tabla de Clientes Tramarsa");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(31, 31, 31)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButton1.setText("Eliminar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(189, 189, 189)
                        .addComponent(jButton1)
                        .addGap(26, 26, 26)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 
        int rowSel=TablaCliente.getSelectedRow();
        int id=Integer.parseInt(TablaCliente.getValueAt(rowSel,0).toString());
         int p= JOptionPane.showConfirmDialog(null,"Desea eliminar la fila seleccionada","Eliminar",JOptionPane.YES_NO_OPTION);
        if (p==0){
            service.eliminaCliente(id);
             Actualizar();        // TODO add your handling code here:
        }
       
    }//GEN-LAST:event_jButton1ActionPerformed

    private void TablaClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaClienteMouseClicked
       int rowSel = TablaCliente.getSelectedRow();
        String idCliente=TablaCliente.getValueAt(rowSel,0).toString();
        String ruc=TablaCliente.getValueAt(rowSel,1).toString();
        String razon=TablaCliente.getValueAt(rowSel,2).toString();
        String dir=TablaCliente.getValueAt(rowSel,3).toString();
        String Rep=TablaCliente.getValueAt(rowSel,4).toString();
        String Lin=TablaCliente.getValueAt(rowSel,5).toString();
        String Dep= TablaCliente.getValueAt(rowSel,6).toString();
        String Ciu=TablaCliente.getValueAt(rowSel,7).toString();
        String Div=TablaCliente.getValueAt(rowSel,8).toString();
        String Pais=TablaCliente.getValueAt(rowSel,9).toString();
        
        Cliente cli=service.queryByCliente(idCliente);
       
    }//GEN-LAST:event_TablaClienteMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablaCliente;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
